package com.hand.hap.demo.controllers;

import com.hand.hap.core.IRequest;
import com.hand.hap.demo.dto.OmOrderLines;
import com.hand.hap.demo.service.IOmOrderLinesService;
import com.hand.hap.system.controllers.BaseController;
import com.hand.hap.system.dto.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 订单行controller
 * @author shuai.wang02@hand-china.com
 * @date  2018/8/25 19:51
 */
@Controller
public class OmOrderLinesController extends BaseController {

    @Autowired
    private IOmOrderLinesService service;


    @RequestMapping(value = "/hap/om/order/lines/query")
    @ResponseBody
    public ResponseData query(OmOrderLines dto, @RequestParam(defaultValue = DEFAULT_PAGE) int page,
                              @RequestParam(defaultValue = DEFAULT_PAGE_SIZE) int pageSize, HttpServletRequest request) {

        IRequest requestContext = createRequestContext(request);
        if (dto.getHeaderId() == null || dto.getHeaderId() == 0) {
            List<OmOrderLines> list = new ArrayList<>();
            return new ResponseData(list);
        }
        return new ResponseData(service.ourSelect(requestContext, dto, page, pageSize));
    }



    @RequestMapping(value = "/hap/om/order/lines/deleteAllSingle")
    @ResponseBody
    public Integer deleteAllSingle(Long headerId, HttpServletRequest request) {
        IRequest requestContext = createRequestContext(request);

        return (service.deleteAllSingle(requestContext, headerId));
    }



    @RequestMapping(value = "/hap/om/order/lines/findMaxLineNumber")
    @ResponseBody
    public Long findMaxLineNumber(Long headerId, HttpServletRequest request) {

        IRequest requestContext = createRequestContext(request);

        return (service.findMaxLineNumber(requestContext, headerId));
    }

    @RequestMapping(value = "/hap/om/order/lines/submit")
    @ResponseBody
    public ResponseData update(@RequestBody List<OmOrderLines> dto, BindingResult result, HttpServletRequest request) {
        getValidator().validate(dto, result);
        if (result.hasErrors()) {
            ResponseData responseData = new ResponseData(false);
            responseData.setMessage(getErrorMessage(result, request));
            return responseData;
        }
        IRequest requestCtx = createRequestContext(request);
        return new ResponseData(service.batchUpdate(requestCtx, dto));
    }

    @RequestMapping(value = "/hap/om/order/lines/remove")
    @ResponseBody
    public ResponseData delete(HttpServletRequest request, @RequestBody List<OmOrderLines> dto) {
        service.batchDelete(dto);
        return new ResponseData();
    }
}